home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 898 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  66 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: lars.farm@nts.mh.se (Lars Farm)
  3. Newsgroups: comp.std.c++
  4. Subject: template and const
  5. Date: 28 Mar 1996 09:31:42 PST
  6. Organization: pv
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <AD8043589668101EA@sleipner.nts.mh.se>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. Content-Type: text/plain; charset=iso-8859-1
  11. Content-Transfer-Encoding: 8bit
  12. X-Original-Date: Thu, 28 Mar 1996 13:23:36 +0100
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMVrM/0y4NqrwXLNJAQH8CAH9EQBxHsTaclpS4OWpnaUS9R3/kcyFT6HB
  15.     9zLl0gOlbGURd+0VMRAxJEBmS/eEmysvxofs/VOovtxTYF0+sej3sA==
  16.     =40b3
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. Is "pair<T1,T2>" valid as function argument where the 
  20. declaration expects "const pair<const T1,T2>&"?
  21.  
  22. This comes up in the standard library map<K,V>::insert and make_pair
  23.  
  24.     template <class T1, class T2>
  25.     struct pair {
  26.       T1 first;
  27.       T2 second;
  28.       pair();
  29.       pair(const T1& x, const T2& y);
  30.     };
  31.  
  32.     template <class T1, class T2> 
  33.     pair<T1,T2> make_pair(const T1&, const T2&);
  34.  
  35.     template <class Key, class T,
  36.               class Compare = less<Key>,
  37.               class Allocator = allocator>
  38.     class map {
  39.       ...
  40.       typedef pair<const Key, T> value_type;
  41.       ...
  42.       pair<iterator, bool> insert(const value_type& x);
  43.       ...
  44.     };
  45.  
  46. void f( string a, string b )
  47. {
  48.     map<string,string> m;
  49.     ...
  50.     m.insert( make_pair(a,b) ); // valid c++?
  51.  
  52. make_pair yields a temporary of type pair<string,string>. temporaries are
  53. const. insert() wants a const pair<const string,string>. So they should
  54. both be a pair of constant strings, right? but the compilers I have access
  55. to disagrees.
  56.  
  57. --
  58. Lars Farm, lars.farm@nts.mh.se
  59. ---
  60. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  61.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  62.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  63.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  64.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  65. ]
  66.